package cn.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.dao.BanjiDao;
import cn.dao.StudentDao;
import cn.dao.TeacherDao;
import cn.entity.TBanji;
import cn.entity.TStudent;
import cn.entity.TTeacher;
import cn.util.PageBean;
@Controller("studentAction")
@Scope("prototype")
public class StudentAction extends BaseAction{
	private Integer id;
	private String name;
	private TStudent student=new TStudent();
	private List<TStudent> list;
	@Autowired
	@Qualifier("studentDao")
	private StudentDao studentDao;
	@Autowired
	@Qualifier("teacherDao")
	private TeacherDao teacherDao;
	
	@Autowired
	@Qualifier("banjiDao")
	private BanjiDao banjiDao;
	
	private PageBean pageBean=new PageBean();
	
	public String list(){
		String sqlCount="select count(*) from TStudent u where 1=1";
		String sql="from TStudent u where 1=1";
		if(name!=null && !"".equals(name)){
			sqlCount+=" and (u.name like '%"+name+"%' or u.stuno like '%"+name+"%')";
			sql+=" and (u.name like '%"+name+"%' or u.stuno like '%"+name+"%')";
		}
		if((Integer)session.getAttribute("role")==3){
			TTeacher teacher=(TTeacher)session.getAttribute("currentUser");
			sqlCount+=" and u.teacher.id="+teacher.getId();
			sql+=" and u.teacher.id="+teacher.getId();
		}
		sqlCount+=" and u.isdel=0";
		sql+=" and u.isdel=0";
		pageBean.setTotalCount(studentDao.getCount(sqlCount));
		list=studentDao.getByPage(sql, pageBean.getPageNo(), pageBean.getPageSize());
		return SUCCESS;
	}
	
	public String add(){
		student.setIsdel(0);
		studentDao.add(student);
		return "login";
	}
	
	public String detail() throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		TStudent stu=studentDao.get(TStudent.class, id);		
		out.print(JSON.toJSONString(stu));
		out.close();
		return null;
//		student=studentDao.get(TStudent.class, id);
//		return "detail";
	}
	
	public String toupdate(){
		student=studentDao.get(TStudent.class, id);
		return "detail";
	}
	
	public String listJson() throws IOException{
		String sql="from TStudent u where u.isdel=0";
		list=studentDao.getByHql(sql);
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.print(JSONArray.toJSONString(list));
		out.close();
		return null;
	}
	
	public String update(){
		Integer role=(Integer)session.getAttribute("role");
		TStudent stu=studentDao.get(TStudent.class, id);
		if(student.getBiyetime()!=null && !"".equals(student.getBiyetime())){
			stu.setBiyetime(student.getBiyetime());
		}
		if(student.getEmail()!=null && !"".equals(student.getEmail())){
			stu.setEmail(student.getEmail());
		}
		if(student.getName()!=null && !"".equals(student.getName())){
			stu.setName(student.getName());
		}
		if(student.getSex()!=null && !"".equals(student.getSex())){
			stu.setSex(student.getSex());
		}
		if(student.getTeacher()!=null && student.getTeacher().getId()!=null){
			stu.setTeacher(teacherDao.get(TTeacher.class, student.getTeacher().getId()));
		}
		if(student.getBanji()!=null && student.getBanji().getId()!=null){
			stu.setBanji(banjiDao.get(TBanji.class, student.getBanji().getId()));
		}
		if(student.getStuno()!=null && !"".equals(student.getStuno())){
			stu.setStuno(student.getStuno());
		}
		
		if(student.getTel()!=null && !"".equals(student.getTel())){
			stu.setTel(student.getTel());
		}
		studentDao.update(stu);
		if(role==1){
			try {
				response.sendRedirect("student_toupdate?id="+stu.getId());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}else{
			return "list";
		}
	}
	
	
	
	public String delete(){
		TStudent s=studentDao.get(TStudent.class, id);
		s.setIsdel(1);
		studentDao.update(s);
		return "list";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public TStudent getStudent() {
		return student;
	}

	public void setStudent(TStudent student) {
		this.student = student;
	}

	public List<TStudent> getList() {
		return list;
	}

	public void setList(List<TStudent> list) {
		this.list = list;
	}

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

}
